import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;

public class MenuFrame extends Frame {

	/**
	 * 
	 */
	private final Color colors[] = {Color.BLACK,Color.DARK_GRAY, Color.GRAY, Color.LIGHT_GRAY, Color.RED, Color.ORANGE,
			Color.MAGENTA, Color.PINK, Color.GREEN, Color.YELLOW, Color.BLUE, Color.CYAN}	;
	private final String colNames[] = {"Black", "Dark Gray" , "Gray", "Light Gray", "Red", "Orange", "Magenta", "Pink", "Green","Yellow", "Blue", "Cyan"};
	private static final long serialVersionUID = 4156494860703412270L;
	private MyCanvas can;
	private Checkbox sqr;
	private Checkbox f;
	private Checkbox cir;
	private boolean w= true; 
	private boolean fill = false;
	private Choice col;
	private Color cc = Color.BLACK;
	private TextField xx;
	private TextField yy;
	private TextField zz;
	private int size = 50;
	private int x = 50; 
	private int y = 50;

	MenuFrame(MyCanvas c){
		super("Menu");
		can = c;
		setLocation(240,200);
		setSize(50, 220);
		setResizable(true);
		setLayout(new BorderLayout());
		
		Button clear = new Button("Clear");
		Button exit = new Button("Exit");
		Button draw = new Button("Draw");

		Panel boxes = new Panel();
		Panel buttons = new Panel();

		Panel xL = new Panel();
		Panel yL = new Panel();
		Panel sL = new Panel();


		xL.setLayout(new GridLayout(1,2));
		xL.add(new Label("X:"));
		xx = new TextField("50");
		xL.add(xx);
		
		yL.setLayout(new GridLayout(1,2));
		yL.add(new Label("Y:"));
		yy = new TextField("50");
		yL.add(yy);
		
		sL.setLayout(new GridLayout(1,2));
		sL.add(new Label("Size:")); 
		 zz = new TextField("50");
		sL.add(zz);
		
		sqr = new Checkbox("Square");
		sqr.setState(true);
		cir = new Checkbox("Circle");
		f = new Checkbox("Filled");
		
		col = new Choice();
		for (int i = 0; i<colNames.length; i++)
			col.addItem(colNames[i]);
		
		boxes.setLayout(new GridLayout(7,1));
		boxes.add(f);
		boxes.add(sqr);
		boxes.add(cir);
		boxes.add(col);
		boxes.add(xL);
		boxes.add(yL);
		boxes.add(sL);
		
		buttons.setLayout(new GridLayout(1,2));
		buttons.add(clear);
		buttons.add(exit);
		
		col.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e) {
				for(int i = 0 ; i< colNames.length; i++)
					if (colNames[i].equals(e.getItem().toString()))
						cc = colors[i];						
			}
		});	
		f.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e) {
				fill = f.getState();
			}
		});	
		
		sqr.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e) {
				w = sqr.getState();
				cir.setState(!sqr.getState());
			}
		});	
		
		cir.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e) {
				w = !cir.getState();
				sqr.setState(!cir.getState());
			}
		});
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}

		});
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				can.clear();
			}

		});
		draw.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(w){
					if (fill)
						can.fillSquare(x,y,size, cc);
					else
						can.drawSquare(x,y,size, cc);
				}
				else{
					if (fill)
						can.fillCircle(x,y,size, cc);
					else
						can.drawCircle(x,y,size, cc);
				}	
			}		

		});
		
		yy.addTextListener(new TextListener(){

			public void textValueChanged(TextEvent arg0) {
				String s = yy.getText();
				if (!s.equals(""))
					if (stringTest(s) != -99)
						y = stringTest(s);		
					else
						yy.setText("");		
			}
		});
		xx.addTextListener(new TextListener(){

			public void textValueChanged(TextEvent arg0) {
				String s = xx.getText();
				if (!s.equals(""))
					if (stringTest(s) != -99)
						x = stringTest(s);		
					else
						xx.setText("");		
			}
		});
		zz.addTextListener(new TextListener(){

			public void textValueChanged(TextEvent arg0) {
				String s = zz.getText();
				if (!s.equals(""))
					if (stringTest(s) != -99)
						size = stringTest(s);		
					else
						zz.setText("");		
			}
		});
		add(boxes, BorderLayout.NORTH);
		add(buttons, BorderLayout.SOUTH);
		add(draw, BorderLayout.CENTER);
		pack();
		setVisible(true);
	}

	public int stringTest(String s){
		
		boolean yes = false;
		if (s != null && !s.equals("") ){
			yes = true;
			for(int i =0; i < s.length();i++)
				yes &= Character.isDigit(s.charAt(i));
		}
		
		return (yes) ? (Integer.parseInt(s)) : -99;
	}



}
